package com.accloud.utils;

import java.security.GeneralSecurityException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AesUtil {
    public static byte[] encrypt(byte[] content, byte[] key) throws GeneralSecurityException {
        if (key == null) {
            return content;
        }
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(1, skeySpec, new IvParameterSpec(key));
        return cipher.doFinal(content);
    }

    public static byte[] decrypt(byte[] content, byte[] key) throws GeneralSecurityException {
        if (key == null) {
            return content;
        }
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(2, skeySpec, new IvParameterSpec(key));
        return cipher.doFinal(content);
    }
}
